﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;


namespace Tesla.Web
{
    public class PhaseServiceDispatcher
    {
        public IList<BaseDC> DisplayAllPhase(int gridSize, ref int? totalRecordCount)
        {

            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; IList<BaseDC> Phases = null;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                Phases = _proxySvcPhase.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return Phases;
        }



        public IList<BaseDC> SearchPhaseByName(string name, int gridSize, ref int? totalRecordCount)
        {
            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; IList<BaseDC> Phases = null;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                Phases = _proxySvcPhase.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return Phases;
        }

        public IList<BaseDC> ReadPhase(int id)
        {
            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; IList<BaseDC> Phases = null;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                Phases = _proxySvcPhase.Read(id);
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return Phases;
        }

        public IList<BaseDC> ReadPhaseByRowNumberRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; IList<BaseDC> Phases = null;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                Phases = _proxySvcPhase.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return Phases;
        }


        public int? CreatePhase(BaseDC bdc)
        {
            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; int? success = 0;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                success = _proxySvcPhase.Create(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return success;
        }

        public int UpdatePhase(BaseDC bdc)
        {
            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; int updateResult = 0;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                updateResult = _proxySvcPhase.Update(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return updateResult;
        }

        public IList<BaseDC> ReadAllProjects()
        {
            ISvcPhase _proxySvcPhase;
            ChannelFactory<ISvcPhase> chfPhase = null; IList<BaseDC> projects = null;

            try
            {
                chfPhase = GADNChannelFactoryProvider.CreateChannelFactory<ISvcPhase>();
                _proxySvcPhase = chfPhase.CreateChannel();
                projects = _proxySvcPhase.ReadAllProjects();
            }
            catch (FaultException gfx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfPhase.State == CommunicationState.Faulted || chfPhase.State == CommunicationState.Opened)
                    chfPhase.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfPhase.Close();
            }
            return projects;
        }
    }
}

